package cn.csc.module.shop.controller.admin.detail;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.shop.controller.admin.detail.vo.*;
import cn.csc.module.shop.controller.app.detail.vo.AppShopDetailRespVO;
import cn.csc.module.shop.convert.detail.ShopDetailConvert;
import cn.csc.module.shop.dal.dataobject.detail.ShopDetailDO;
import cn.csc.module.shop.service.detail.ShopDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static cn.csc.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "管理后台 - 店铺详情")
@RestController
@RequestMapping("/shop/detail")
@Validated
public class ShopDetailController {

    @Resource
    private ShopDetailService detailService;

    @PostMapping("/create")
    @ApiOperation("创建店铺详情")
    @PreAuthorize("@ss.hasPermission('shop:detail:create')")
    public CommonResult<BigInteger> createDetail(@Valid @RequestBody ShopDetailCreateReqVO createReqVO) {
        return success(detailService.createDetail(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新店铺详情")
    @PreAuthorize("@ss.hasPermission('shop:detail:update')")
    public CommonResult<Boolean> updateDetail(@Valid @RequestBody ShopDetailUpdateReqVO updateReqVO) {
        detailService.updateDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除店铺详情")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:detail:delete')")
    public CommonResult<Boolean> deleteDetail(@RequestParam("id") BigInteger id) {
        detailService.deleteDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得店铺详情")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:detail:query')")
    public CommonResult<ShopDetailRespVO> getDetail(@RequestParam("id") BigInteger id) {
        ShopDetailDO detail = detailService.getDetail(id);
        return success(ShopDetailConvert.INSTANCE.convert(detail));
    }

    @GetMapping("/getCurrentUser")
    @ApiOperation("获得当前用户的店铺详情")
    public CommonResult<ShopDetailRespVO> getCurrentUser() {
        ShopDetailDO detail = detailService.getDetailByUserId(BigInteger.valueOf(getLoginUserId()));
        return success(ShopDetailConvert.INSTANCE.convert(detail));
    }

    @GetMapping("/list")
    @ApiOperation("获得店铺详情列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('shop:detail:query')")
    public CommonResult<List<ShopDetailRespVO>> getDetailList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<ShopDetailDO> list = detailService.getDetailList(ids);
        return success(ShopDetailConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得店铺详情分页")
    @PreAuthorize("@ss.hasPermission('shop:detail:query')")
    public CommonResult<PageResult<ShopDetailRespVO>> getDetailPage(@Valid ShopDetailPageReqVO pageVO) {
        PageResult<ShopDetailDO> pageResult = detailService.getDetailPage(pageVO);
        return success(ShopDetailConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/pageWithAuditTime")
    @PreAuthorize("@ss.hasPermission('shop:detail:query')")
    public CommonResult<PageResult<ShopDetailWithAuditTimeRespVO>> getDetailPageWithAuditTime(@Valid ShopDetailPageReqVO pageVO) {
        PageResult<ShopDetailWithAuditTimeRespVO> pageResult = detailService.getPageWithAuditTime(pageVO);
        return success(pageResult);
    }


    @GetMapping("/pageOfSimple")
    @ApiOperation("获得店铺详情分页")
    @PreAuthorize("@ss.hasPermission('shop:detail:query')")
    public CommonResult<PageResult<AppShopDetailRespVO>> getSimpleDetailPage(@Valid ShopDetailPageReqVO pageVO) {
        PageResult<AppShopDetailRespVO> pageResult = detailService.getSimpleDetailPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出店铺详情 Excel")
    @PreAuthorize("@ss.hasPermission('shop:detail:export')")
    @OperateLog(type = EXPORT)
    public void exportDetailExcel(@Valid ShopDetailExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopDetailDO> list = detailService.getDetailList(exportReqVO);
        // 导出 Excel
        List<ShopDetailExcelVO> datas = ShopDetailConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "店铺详情.xls", "数据", ShopDetailExcelVO.class, datas);
    }

}
